<template>
	<div class="main">
		<div class="main-header" style="display: flex;justify-content: space-between;">
			<!-- <el-button
                v-for="(item, index) in buttons"
                :key="index"
                :type="item.path | formatType"
                :icon="item.icon"
                :disabled="item.path | formatDisabled(selection)"
                @click="tapButton(item)"
                >{{ item.title }}</el-button
            > -->
			<div>
				<div style="display: flex;">
					<el-input placeholder="请输入(公司名称等)" v-model="keyword" class="input-with-select">
						<!-- <el-button slot="append" icon="el-icon-search"  @click="searchinfo()"></el-button> -->
					</el-input>
					<el-button type="primary" style="margin: 0 10px;" @click="searchinfo()">搜索</el-button>
				</div>
			</div>
			<div style="display: flex;">
				<el-button type="primary" v-if="findButton('add')" @click="addcustomer()">添加</el-button>
				<el-button type="primary" v-if="findButton('del')" @click="selectdel()">批量删除</el-button>
				<!-- <el-upload-file :showfile="false" v-model="importindex"  @click="getImportData()"
				accept=".xlsx" style="margin: 0 10px;" title="导入"></el-upload-file> -->
			</div>
		</div>
		<!--  @row-click="onRowClick" -->
		<el-table ref="table" class="main-table" :data="memberData" height="100%" border
			header-cell-class-name="main-table-header" @selection-change="onSelectionChange">
			<el-table-column type="selection" width="40"></el-table-column>
			<el-table-column align="center"  width="150" label="公司名称" prop="work_name" />
			<el-table-column align="center" label="法人姓名" prop="legal_person" />
			<el-table-column align="center" label="公司电话" prop="work_mobile" />
			<el-table-column align="center" label="公司地址" prop="work_address" />
			<el-table-column align="center" label="公司邮箱" prop="work_email" />
			<el-table-column align="center" label="联系人姓名" prop="lx_name" />
			<el-table-column align="center" label="联系人职务" prop="lx_work" />
			<el-table-column align="center" label="联系人电话" prop="lx_mobile" />
			<el-table-column align="center" label="联系人邮箱" prop="lx_email" />
			<el-table-column align="center" label="公司网址" prop="work_site" />
			<el-table-column align="center" label="公司性质" prop="work_ment" >
			<template slot-scope="scope">
				{{work_ment[scope.row.work_ment-1]}}
			</template>
			</el-table-column>
			<el-table-column align="center" label="申请入库分类" prop="cate_name" >
				
			</el-table-column>
			<el-table-column align="center" label="公司经营范围" prop="jy_scope" />
			<el-table-column align="center" label="公司资质" prop="work_level" />
			<el-table-column align="center" label="审核状态" prop="work_ment" >
			<template slot-scope="scope">
				<div v-if="scope.row.apply_status==1">待审核</div>
				<div v-if="scope.row.apply_status==2">已通过</div>
				<div v-if="scope.row.apply_status==3">已拒绝</div>
			</template>
			</el-table-column>
			<!-- <el-table-column align="center" label="公司人数" prop="people_num" /> -->
			<!-- <el-table-column align="center" label="公司占地（办公）面积" prop="area" /> -->
			<el-table-column align="center" label="操作" min-width="150px">
				<template slot-scope="scope" >
					<!-- apply_status 1待审核2通过3拒绝 -->
					<el-button class="butbox" v-if="findButton('edit')" type="primary" @click.stop="addcustomer(scope.row)" size="small">编辑</el-button>
					<el-button class="butbox" v-if="findButton('del')" type="primary" @click.stop="del(scope.row)" size="small">删除</el-button>
					<el-button class="butbox"  v-if="findButton('pass')&&scope.row.apply_status==1" type="primary" @click.stop="passsupplier(scope.row)" size="small">审核</el-button>
					<el-button class="butbox" v-if="findButton('passed')&&scope.row.apply_status==2" type="primary" disabled size="small">已通过</el-button>
					<el-button class="butbox" v-if="findButton('unpassed')&&scope.row.apply_status==3" type="primary" disabled size="small">已拒绝</el-button>
					<!-- <el-button class="butbox" type="primary" @click.stop="passsupplier(scope.row)" size="small">拒绝</el-button> -->
				</template>
			</el-table-column>
		</el-table>
		<el-pagination class="main-pagination" :current-page.sync="page" :page-size="limit" :total="total"
			@current-change="onCurrentChange" layout="total,jumper,prev,pager,next"></el-pagination>
		<el-dialog :visible.sync="dialogVisible" :key="formDataStatus" :title="dialogTitle" append-to-body width="1100px"
			@close="onDialogClose">
			<component :is="dialogView" @update="getMemberData" @search="addsuppler" :selection="selection"
			:formData="formData" :catetype="catetype" :looking="looking"></component>
		</el-dialog>
		<!-- <Info ref="info" :selection="selection" @close="onDialogClose" /> -->
	</div>
</template>

<script>
	import {
		get,post,del
	} from "@/assets/js/http.js";
	export default {
		name: "view-supplier",
		data() {
			return {
				memberData: [],
				selection: [],
				page: 1,
				limit: 20,
				total: 0,
				dialogVisible: false,
				dialogTitle: "",
				dialogView: "",
				work_ment:['国有企业','私营企业','外资企业','合资企业','其它'],//公司性质1国有2私营3外资4合资企业5其它
				formData:{},
				ids:[],//批量删除
				keyword:'',
				formDataStatus:false,
				work_time:'',
				passids:[],
				looking:false,
				catetype:[],
			};
		},
		computed: {
			buttons() {
				return this.$store.state.buttons;
			}
		},
		components: {
			// Photo: () => import("./photo.vue"),
			Add: () => import("./add.vue"),
			// Info: () => import("./info.vue")
		},
		mounted() {
			this.getMemberData();
			this.getcatelist()
		},
		methods: {
			//添加弹窗
			addcustomer(info) {
				if (info) {
					this.dialogTitle = "编辑";
					if(Number.isFinite(info.work_time)){
						this.work_time = new Date(info.work_time*1000)
						info.work_time = this.work_time
					}
					//info.work_time = this.formatDateT(info.work_time*1000),
					this.formData = info
					this.looking = true//不显示账号密码
				} else {
					this.looking = false//显示账号密码
					this.dialogTitle = "添加";
					this.formData ={}
				}
				this.dialogView = "Add";
				this.formDataStatus = true
				this.dialogVisible = true;
			},
			//编辑，添加
			async addsuppler(result) {
				const infos = JSON.parse(JSON.stringify(result));
				this.page = 1;
				if(this.dialogTitle=='编辑'){
					await post({
						url:'/admin/customer.Supplier/edit',
						data:infos
					})
				}else{
					await post({
						url:'/admin/customer.Supplier/add',
						data:infos
					})
				}
				
				this.getMemberData()
			},
			//时间戳转中国时间
			formatDateT(dataTime){
						// var timestamp3 = 1551686227000; 
						var timestamp = dataTime;
						var newDate = new Date(dataTime ); 
						// var newDate = new Date(dataTime + 8 * 3600 * 1000 );               
						newDate.getTime(timestamp * 1000);                
						// console.log(newDate.toDateString());//Mon Mar 11 2019                
						// console.log(newDate.toGMTString()); //Mon, 11 Mar 2019 06:55:07 GMT                
						// console.log(newDate.toISOString()); //2019-03-11T06:55:07.622Z
						return newDate.toISOString()
					},
			//获取列表
			async getMemberData() {
				const data = await get({
					url: "/admin/customer.Supplier/index",
					loading: true,
					params: {
						page: this.page,
						limit: this.limit,
						keyword:this.keyword
					}
				});
				const list = data.data;
				this.total = data.total;
				this.memberData = list;
				this.keyword=""
			},
			//批量删除
			async selectdel(){
				if(!this.selection){
					this.$message('请选择')
				}else{
					for(let i=0;i<this.selection.length;i++){
						this.ids.push(this.selection[i].id)
					}
					console.log(this.ids)
					this.$confirm("此操作将永久删除文件, 是否继续?", "提示", {
							confirmButtonText: "确定",
							cancelButtonText: "取消",
							type: "warning",
						})
						.then(async () => {
							let query = "?";
							for (let i = 0; i < this.ids.length; i++) {
								const item = this.ids[i];
								// console.log(item, 123);
								if (i > 0) {
									query += "&ids[]=" + item;
								} else {
									query += "ids[]=" + item;
								}
							}
							await del({
								url: "/admin/customer.Supplier/del" + query,
								successTip: true,
								loading: true,
							});
							this.ids=[]
							this.getMemberData();
						})
						.catch(() => {
							this.$message({
								type: "info",
								message: "已取消删除",
							});
						});
				}
			},
			//删除
			del(row) {
				console.log("del", row);
				this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
						confirmButtonText: "确定",
						cancelButtonText: "取消",
						type: "warning",
					})
					.then(async () => {
						let ids = [];
						ids.push(row.id);
						ids.map(String);
						console.log(ids, "ids");
						let query = "?";
						for (let i = 0; i < ids.length; i++) {
							const item = ids[i];
							// console.log(item, 123);
							if (i > 0) {
								query += "&ids[]=" + item;
							} else {
								query += "ids[]=" + item;
							}
						}
						await del({
							url: "/admin/customer.Supplier/del" + query,
							successTip: true,
							loading: true,
						});
						this.getMemberData();
					})
					.catch(() => {
						this.$message({
							type: "info",
							message: "已取消删除",
						});
					});
			
			},
			//分类列表
			async getcatelist(){
				await post({
					url:'/admin/customer.Supplier/cate'
				}).then(res=>{
					this.catetype = res
					// console.log(this.catetype)
				})
			},
			//分页
			onCurrentChange(index) {
				this.page = index;
				this.getMemberData();
			},
			//搜索
			searchinfo(){
				if(!this.keyword){
					this.$message('搜索词不能为空')
				}
				this.page = 1
				this.keyword = this.keyword.trim()
				this.getMemberData();
			},
			passsupplier(row){
				this.passids.push(row.id)
				console.log(row,this.passids)
				this.$confirm("是否要通过或拒绝该条申请?", "提示", {
						confirmButtonText: "通过",
						cancelButtonText: "拒绝",
						type: "warning",
					})
					.then(async () => {
						//apply_status1待审核2通过3拒绝
						await post({
							url: "/admin/customer.Supplier/process",
							data:{
								apply_status:2,
								id:this.passids
							},
							successTip: true,
							loading: true,
						});
						this.getMemberData();
					})
					.catch(async() => {
						await post({
							url: "/admin/customer.Supplier/process",
							data:{
								apply_status:3,
								id:this.passids
							},
							successTip: true,
							loading: true,
						});
						this.getMemberData();
					});
			},
			onSearch() {
				this.dialogTitle = "搜索";
				this.dialogView = "Search";
				this.dialogVisible = true;
			},
			//选择
			onSelectionChange(selection) {
				this.selection = selection;
			},
			onCheck() {
				// this.dialogTitle = "查看客户照片";
				// this.dialogView = "Photo";
				// this.dialogView = "Info";
				// this.dialogVisible = true;
				// this.$refs.info.show();
			},
			onRowClick() {
				// this.$refs.table.toggleRowSelection(row);
				// this.$refs.info.show();
			},
			onDialogClose() {
				this.formData = {}
				this.$refs.table.clearSelection();
			},
			tapButton(item) {
				if (item.path === "check") {
					this.onCheck();
				} else if (item.path === "search") {
					this.onSearch();
				}
			}
		}
	};
</script>
<style  scoped>
	.butbox{
		margin: 10px;
	}
</style>
